package priv.liaohaolong.fullstacktest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.liaohaolong.fullstacktest.service.CaptchaService;
import priv.liaohaolong.fullstacktest.vo.CaptchaVO;

import java.io.IOException;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    private final CaptchaService captchaService;

    @Autowired
    public CaptchaController(CaptchaService captchaService) {
        this.captchaService = captchaService;
    }

    @GetMapping
    public CaptchaVO captcha() throws IOException {
        return captchaService.create();
    }

    /**
     * 每分钟清除一次过期的验证码关联记录
     */
    @Scheduled(cron = "0 * * * * ?")
    public void clearExpired() {
        captchaService.clearExpired();
    }

}
